import {
  Controller,
  Get,
  Post,
  Body,
  Query,
  Param,
  Req,
  ValidationPipe,
  Logger,
  Version,
} from '@nestjs/common';
import { OrderService } from './order.service';
import { SuccessResponse } from '../common/response/success-response';
import { ErrorResponse } from '../common/response/error-response';
import { ResponseInterface } from '../common/response/response.interface';
import { SaveOrderDto, DelOrderDto } from './dto/order.dto';

@Controller('order')
export class OrderController {
  constructor(private readonly orderService: OrderService) {} // 注入服务层

  @Get('detail/:id') // 如 /order/detail/123
  async getOrderDetail(@Param() params): Promise<ResponseInterface> {
    try {
      const orderResponse = await this.orderService.getOrderDetail(params.id);
      return new SuccessResponse(orderResponse);
    } catch (e: any) {
      return new ErrorResponse(e.message);
    }
  }

  @Post('save') // 如 /order/save
  async saveOrder(
    @Body(new ValidationPipe()) dto: SaveOrderDto,
  ): Promise<ResponseInterface> {
    // 引入ValidationPipe，自动验证表单参数
    try {
      return new SuccessResponse(await this.orderService.saveOrder(dto));
    } catch (e: any) {
      return new ErrorResponse(e);
    }
  }

  @Post('del') // 如 /order/del
  async delOrderDetail(
    @Body(new ValidationPipe()) dto: DelOrderDto,
  ): Promise<ResponseInterface> {
    try {
      await this.orderService.removeOrder(dto.id);
      return new SuccessResponse({ id: dto.id });
    } catch (e: any) {
      return new ErrorResponse(e.message);
    }
  }
}
